package com.aaa.sys.controller;

import com.aaa.common.entity.User;
import com.aaa.common.util.BaseUtil;
import com.aaa.common.util.OssUtil;
import com.aaa.common.util.Result;
import com.aaa.common.vo.Page;
import com.aaa.sys.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2025-09-03 19:03:33
 */
@RestController
@RequestMapping("user")
@Log4j2
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile file){
        //调用工具类，进行上传
        return BaseUtil.success(OssUtil.uploadFile("user/images",file));
    }

    @PostMapping("checkPassword")
    public Result checkPassword(String password) {
        return BaseUtil.success(this.userService.checkPassword(password));
    }

    @PostMapping("updatePassword")
    public Result updatePassword(String oldPassword, String newPassword) {
        return BaseUtil.success(this.userService.updatePassword(oldPassword, newPassword));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne() {
        return BaseUtil.success(this.userService.selectOne());
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<User> page) {
        return BaseUtil.success(this.userService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public User queryById(@RequestParam("id") Integer id) {
        return this.userService.queryById(id);
    }

    @GetMapping("queryByUserName")
    public User queryByUserName(@RequestParam("userName") String userName) {
        return this.userService.queryByUserName(userName);
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody User user) {
        return BaseUtil.success(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody User user) {
        return BaseUtil.success(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.userService.deleteById(id));
    }

}

